/**
 * Right click plugin for jquery
 * @author: Gavin Buske
 * 
 */
(function ($) {
    var data = {}
    var methods = {
        rightclick: function(evt, obj){
            selector = $(obj).attr('class');
            if (evt.which==3) {
                $ul = $('<ul>').css({
                    'position':'absolute', 'left':evt.clientX, 'top':evt.clientY,
                    'list-style':'none', 'background':'#CCC', 'border':'1px solid grey',
                    'padding':'10px', 'cursor':'pointer', 'width':'80px'
                });
                $ul.append($('<li>').css({'border-bottom':'1px solid grey'}));
                for (var i in data[selector].menu) {
                    if (data[selector].menu[i].name=="_sep") {
                        data[selector].menu[i].name = "";
                    }
                    $newlm = $('<li>').css({
                        'border-bottom':'1px solid grey'
                    }).attr('data-id', i);
                    $newlm.html(data[selector].menu[i].name).click(function(){
                        $(this).parent().remove();
                        var indx = $(this).attr('data-id');
                        if ( (typeof data[selector].menu[indx].settings != "undefined" && data[selector].menu[indx].settings.passthis==true) || (typeof data[selector].settings != "undefined" && data[selector].settings.passthis==true) ){
                            if (typeof data[selector].menu[indx].call != "undefined") data[selector].menu[indx].call(obj);
                        } else {
                            if (typeof data[selector].menu[indx].call != "undefined") data[selector].menu[indx].call();
                        }
                    }).hover(function(){$(this).css('background', 'white');},
                        function() {$(this).css('background', '#CCC')}
                    );
                    $ul.append($newlm);
                    $(document).click(function(evt){
                        var left = $ul.position().left;
                        var right = left + $ul.width();
                        var top = $ul.position().top;
                        var botm = top + $ul.height();
                        var wc = (evt.clientX < left) || (evt.clientX > right);
                        var hc = (evt.clientY < top) || (evt.clientY > botm);
                        if (wc==true || hc==true) {
                            $ul.remove();
                        }
                    });
                }
                $ul.css('box-shadow', '2px 2px 4px black');
                $('body').append($ul);
            }
        },
        setvars: function(vars, obj) {
            selector = $(obj).attr('class');
            data[selector] = {menu:'', settings:''}
            if (typeof vars.menu!="undefined") data[selector].menu = vars.menu;
            if (typeof vars.settings!="undefined") data[selector].settings = vars.settings;
        }
    }
    $.fn.gb_rightclick = function(varsobj) {
        this.bind('contextmenu', function(evt) {methods.rightclick(evt, this); return false;});
        if (typeof varsobj != "undefined") {
            methods.setvars(varsobj, this);
        }
    }
})(jQuery);

/**
 * DOCS and NOTES
 * ---------------
 * 
 * Multiple plugin instantiation
 * ------------------------------
 * Using plugin with multiple selectors is possible, perhaps if you want to specify a different menu for each
 * selector type. However, it currently only supports class selectors (or selectors whose matching elements have
 * unique classes).
 * Example:
 * $('.files').gb_rightclick(menu1);
 * $('.folders').gb_rightclick(menu2);
 * 
 * 
 */